<?php
/**
 * @Updated by temniy ,  http://sitelab.net.ru ,  2009 aug.
 *
 * JumpTo - A Download System for Content Pages. Work in Joomla 1.0.x & Joostina.
 * @author:      Лаборатория сайтов
 * @version:     1.3
 * @release:     2009.12.20
 * @package:     com_jumpto
 * @copyright:   (C) 2009 http://sitelab.net.ru
 * @license:     http://www.gnu.org/copyleft/gpl.html GNU/GPL
 *
 * Cоздан на базе Registered Downloads  (C) 2006 by St. John Johnson
 **/

#-Запрет прямого доступа-#
defined( '_VALID_MOS' ) or die();

if (file_exists( JPATH_BASE.'/components/com_jumpto/lang/' . $mosConfig_lang . '.php')){
	include_once( JPATH_BASE.'/components/com_jumpto/lang/' . $mosConfig_lang . '.php' );
}else{
	include_once( JPATH_BASE.'/components/com_jumpto/lang/russian.php' );
}

$uid = mosGetParam( $_REQUEST, 'uid', '' );
$cid = mosGetParam( $_REQUEST, 'cid', '' );

require_once( JPATH_BASE."/administrator/components/com_jumpto/class.jumpto.php" );
require_once( $mainframe->getPath( 'admin_html' ) );

switch ($task) {
	case "new":
		editFiles( $option, 0 );
		break;

	case "new":
		newFiles( $option );
		break;

	case "edit":
		editFiles( $option,  $cid );
		break;

	case "save":
		saveFiles( $option );
		break;

	case "remove":
		deleteFiles( $cid, $option );
		break;

	default:
		showFiles( $option );
		break;
}

/**
 *
 * @global <type> $mosConfig_list_limit
 * @param <type> $option
 * @return <type>
 */
function showFiles ( $option ) {
	global $mosConfig_list_limit;

	$mainframe = mosMainFrame::getInstance();
	$database = $mainframe->getDBO();

	$limit = intval($mainframe->getUserStateFromRequest("viewlistlimit",'limit',$mosConfig_list_limit));
	$limitstart = intval($mainframe->getUserStateFromRequest("view{$option}limitstart",'limitstart',0));
	$search     = $mainframe->getUserStateFromRequest( "search{$option}", 'search', '' );
	$search = (get_magic_quotes_gpc()) ? stripslashes($search) : $search;
	
	$where = array();
	if ($search) {
		$where[] = "LOWER(name) LIKE '%".$database->getEscaped(trim(strtolower($search)))."%'";
	}

	$total = $database->setQuery( "SELECT count(*) FROM #__jumpto AS a" . (count( $where ) ? "\nWHERE " . implode( ' AND ', $where ) : "") )->loadResult();

	echo $database->getErrorMsg();

	include_once( "includes/pageNavigation.php" );
	$pageNav = new mosPageNav( $total, $limitstart, $limit  );

	$database->setQuery( "SELECT * FROM #__jumpto"
			. (count( $where ) ? "\nWHERE " . implode( ' AND ', $where ) : "")
			. "\nORDER BY id DESC"
			. "\nLIMIT $pageNav->limitstart,$pageNav->limit");
	$rows = $database->loadObjectList();
	
	if ($database->getErrorNum()) {
		echo $database->stderr();
		return false;
	}

	JumpTo_HTML::showFiles( $option, $rows, $search, $pageNav );
}

/**
 *
 * @param <type> $option
 */
function newFiles( $option ) {
	$database = database::getInstance();

	$row = new mosJumpTo( $database );
	$row->load( $uid );

	if ($uid) {
		$row->checkout( $my->id );
	} else {
		$row->published = 0;
	}

	JumpTo_HTML::newFiles( $option, $row );
}

/**
 *
 * @global <type> $my
 * @param <type> $option
 * @param <type> $cid
 */
function editFiles( $option, $cid ) {
	global $my;
	$database = database::getInstance();

	$row = new mosJumpTo( $database );
	$row->load( (int)$cid );

	if ($cid) {
		$row->checkout( $my->id );
	} else {
		$row->published = 0;
	}

	JumpTo_HTML::editFiles( $option, $row );
}

/**
 *
 * @param <type> $option 
 */
function saveFiles( $option ) {
	$database = database::getInstance();

	$row = new mosJumpTo( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	mosRedirect( "index2.php?option=$option" );
}

/**
 *
 * @param <type> $cid
 * @param <type> $option 
 */
function deleteFiles( $cid, $option ) {
	$database = database::getInstance();
	if (count( $cid )) {
		$cids = implode( ',', $cid );
		$database->setQuery( "DELETE FROM #__jumpto WHERE id IN ($cids)" );
		if (!$database->query()) {
			echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
		}
	}
	mosRedirect( "index2.php?option=$option" );
}